/***********************************************************
		Group			:		G2
		Project         :       Big Table
		File Name       :       Stream.java
		Author			:		Preetika Tyagi
		Purpose         :       To initialize stream of maps 
		Change History  :
 ***********************************************************/
package BigT;

import global.AttrOperator;
import global.AttrType;
import global.GlobalConst;
import global.IndexType;
import global.MID;
import index.IndexException;
import index.IndexScan;
import iterator.CondExpr;
import iterator.FldSpec;
import iterator.RelSpec;
import iterator.UnknownKeyTypeException;

import java.io.IOException;

public class Stream {

	private IndexScan iscan = null;
	private String rowFilter = "";
	private String columnFilter = "";
	private String valueFilter = "";
	private int orderType = 1;

	public Stream(bigt bigTable, String bigTName, int type, int orderType,
			String rowFilter, String columnFilter, String valueFilter,
			int numBuf) {
		if (rowFilter != null) {
			this.rowFilter = rowFilter;
		}
		if (columnFilter != null) {
			this.columnFilter = columnFilter;
		}
		if (valueFilter != null) {
			this.valueFilter = valueFilter;
		}
		this.orderType = orderType;
		bigTable.bigt(bigTName, type, numBuf);
		AttrType[] attrType = new AttrType[4];
		attrType[0] = new AttrType(AttrType.attrString);
		attrType[1] = new AttrType(AttrType.attrString);
		attrType[2] = new AttrType(AttrType.attrInteger);
		attrType[3] = new AttrType(AttrType.attrString);
		short[] attrSize = new short[3];
		attrSize[0] = 32;
		attrSize[1] = 32;
		attrSize[2] = 32;
		FldSpec[] projlist = new FldSpec[4];
		RelSpec rel = new RelSpec(RelSpec.outer);
		projlist[0] = new FldSpec(rel, 1);
		projlist[1] = new FldSpec(rel, 2);
		projlist[2] = new FldSpec(rel, 3);
		projlist[3] = new FldSpec(rel, 4);
		CondExpr[] expr = new CondExpr[2];
		expr[0] = new CondExpr();
		expr[0].op = new AttrOperator(AttrOperator.aopEQ);
		expr[0].type1 = new AttrType(AttrType.attrSymbol);
		expr[0].type2 = new AttrType(AttrType.attrString);
		expr[0].next = null;
		expr[1] = null;
		String indexFileToUse = null;
		if (orderType == 1) {
			indexFileToUse = "BTreeIndex1";
			expr[0].operand1.symbol = new FldSpec(new RelSpec(RelSpec.outer), 1);
			expr[0].operand2.string = this.rowFilter + "#" + this.columnFilter;
			if (this.rowFilter == "") {
				expr = null;
			}
		}
		if (orderType == 2) {
			indexFileToUse = "BTreeIndex2";
			expr[0].operand1.symbol = new FldSpec(new RelSpec(RelSpec.outer), 2);
			expr[0].operand2.string = this.columnFilter + "#" + this.rowFilter;
			if (this.columnFilter == "") {
				expr = null;
			}
		}
		if (orderType == 3) {
			indexFileToUse = "BTreeIndex3";
			expr[0].operand1.symbol = new FldSpec(new RelSpec(RelSpec.outer), 1);
			expr[0].operand2.string = this.rowFilter;
			if (this.rowFilter == "") {
				expr = null;
			}
		}
		if (orderType == 4) {
			indexFileToUse = "BTreeIndex4";
			expr[0].operand1.symbol = new FldSpec(new RelSpec(RelSpec.outer), 2);
			expr[0].operand2.string = this.columnFilter;
			if (this.columnFilter == "") {
				expr = null;
			}
		}
		if (orderType == 5) {
			indexFileToUse = "BTreeIndex5";
			expr = null;
		}
		expr=null;
		try {
			iscan = new IndexScan(new IndexType(IndexType.B_Index),
					GlobalConst.BigTHeapFile, indexFileToUse, attrType,
					attrSize, 4, 4, projlist, expr, 1, false); // last parameter
			// is true if
			// only
			// index entry is to
			// be fetched instead of whole map
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void closestream() {
		try {
			iscan.close();
		} catch (IndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Map getNext(MID mid) {
		Map nextMap = null;
		try {
			nextMap = iscan.get_next(this.orderType, this.rowFilter,
					this.columnFilter, this.valueFilter);
		} catch (IndexException e) {
			e.printStackTrace();
		} catch (UnknownKeyTypeException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return nextMap;
	}
}
